package com.gxa.user.service.order.impl;/**
 * @author JunYu
 * @create 2022-10-08--10:03
 */


import com.gxa.api.shop.ShopApi;
import com.gxa.backend.api.ActivityManagementApi;
import com.gxa.common.result.R;
import com.gxa.common.utils.SnowFlower;

import com.gxa.user.mapper.OrderMapper;
import com.gxa.user.pojo.order.OrderGoods;
import com.gxa.user.pojo.order.SysOrder;

import com.gxa.user.redis.timedtask.GoodNumUpdate;
import com.gxa.user.redis.utils.RedisLockUtils;
import com.gxa.user.service.order.OrderService;
import com.gxa.user.service.order.util.GenerateDiscountPrice;
import com.gxa.user.service.order.util.GenerateOrderByOrderInfo;
import com.gxa.user.vo.OrderInfo;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * @program: take-out-project
 * @description: 实现类
 * @author: JunYu
 * @create: 2022-10-08 10:03
 * //
 **/
@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    RedisLockUtils redisLockUtils;
    @Autowired
    OrderMapper mapper;
    @Autowired
    ShopApi shopApi;
    @Autowired(required = false)
    GoodNumUpdate goodNumUpdate;
    @Autowired(required = false)
    ActivityManagementApi activityManagementApi;

    @Override
    public R createTakeOutOrder(OrderInfo orderInfo) {
        RedisTemplate redisTemplate = redisLockUtils.getRedisTemplate();
        R r = new R();
        List<OrderGoods> orderGoodsList = orderInfo.getOrderGoods();
        SnowFlower snowFlower = SnowFlower.getInstance();

        for (OrderGoods orderGoods : orderGoodsList) {
            //1. 获取redis中的商品数量信息
            Integer amountInDB = Integer.getInteger((String) redisTemplate.opsForHash().get("GoodIdAndNum", orderGoods.getGoodsId()));
            if (amountInDB < orderGoods.getAmount()) {
                //若redis中库存不够，就先去调用商家库检查一下
                amountInDB = goodNumUpdate.updateGoodNumByGoodId(orderGoods.getGoodsId());
                if (amountInDB < orderGoods.getAmount()) {
                    r.setCode(500);
                    r.setMessage("库存不够");
                    return r;
                } else {
                    //修改redis里的库存
                    Integer afterAmount = amountInDB - orderGoods.getAmount();
                    do {
                        if (redisLockUtils.getLock("OpsGoodIdAndNum", "Free")) {
                            redisTemplate.opsForHash().put("GoodIdAndNum", orderGoods.getGoodsId(), afterAmount);
                            break;
                        }
                    } while (true);
                }
            } else {
                Integer afterAmount = amountInDB - orderGoods.getAmount();
                //修改redis里的库存
                do {
                    if (redisLockUtils.getLock("OpsGoodIdAndNum", "Free")) {
                        redisTemplate.opsForHash().put("GoodIdAndNum", orderGoods.getGoodsId(), afterAmount);
                        break;
                    }
                } while (true);
            }
        }
        //生成订单id
        long orderId = snowFlower.generateId();
        //生成初版订单
        SysOrder sysOrder = GenerateOrderByOrderInfo.generateOrderByOrderInfo(orderInfo,orderId);
        //设置订单商品List中的订单id,以及订单总额
        BigDecimal totalPriceInOrder = new BigDecimal(0);
        for(OrderGoods orderGoods:orderGoodsList)
        {
            orderGoods.setOrderId(orderId);
            totalPriceInOrder.add(orderGoods.getTotalPrice());
        }
        //设置总优惠价格
            //查询优惠(类型)
        Integer discountType = activityManagementApi.findGoodIdAndNum(orderInfo.getDiscountId());
        //计算优惠价格
        BigDecimal discountPrice = GenerateDiscountPrice.getDiscountPrice(discountType, totalPriceInOrder);
        //设置优惠价格
        sysOrder.setTotalDiscount(discountPrice);
        //设置应付款
        //设置支付方式


        return null;
    }
}
